package it.units.dacuts.image;

import it.units.dacuts.utility.Check;

/**
 * Utility enumerator class to manage ARGB color model.
 */
public enum ARGBChannel {

    /**
     * The alpha channel.
     */
    ALPHA(3),
    
    /**
     * The red channel.
     */
    RED(2),
    
    /**
     * The green channel.
     */
    GREEN(1),
    
    /**
     * The blue channel.
     */
    BLUE(0);
    
    private final int shift;
    private final int mask;

    private ARGBChannel(int byteIndex) {
        shift = Byte.SIZE * byteIndex;
        mask = 0xFF << shift;
    }

    /**
     * Returns the value of the channel obtained from an ARGB value.
     * 
     * @param argb the ARGB value
     * @return the value of the channel
     */
    public int getValueFromARGB(int argb) {
        return (argb & mask) >>> shift;
    }

    /**
     * Substitutes the channel value of an ARGB value.
     * 
     * @param value the new value of the channel
     * @param argb the ARGB value to modify
     * @return the modified ARGB value
     * @throws IllegalArgumentException if {@code value} is not inside the
     *         unsigned byte range
     */
    public int setValueToARGB(int value, int argb) {
        Check.argument(value >= 0x0 && value < 0x100, "channel value is not in the byte range");
        return (argb & ~mask) | (value << shift);
    }
}
